﻿using e_commerce.BLL;
using e_commerce.Foreground.Models;
using e_commerce.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Mail;
using System.Web;
using System.Web.Mvc;
using e_commerce.BLL.Util;

namespace e_commerce.Foreground.Controllers
{
    public class AccountController : Controller
    {
        private AccountService _aService;
        private Mei_Mail mmail;

        public AccountController()
        {
            _aService = new AccountService();
            mmail = new Mei_Mail();
        }
        // GET: Account
        public ActionResult Login(string callback)
        {
            
            return View();
        }

        [HttpPost]
        public ActionResult CheckLogin(string account, string password,string url = "/Page/Home")
        {
            try
            {
                var client = _aService.ClientLogin(account, password);
                ClientModel IdentityClient = new ClientModel()
                {
                    Name = client.User.NickName,
                    Password = client.User.Password,
                    E_mail = client.User.Email,
                    Wallet = client.Wallet,
                    Gender = client.User.Gender == false ? "女" : "男",
                    BirthDay = client.User.Birthday.ToString(),
                    ClientId = client.ClientId,
                    PhoneNumber = client.User.PhoneNumber,
                };
                Session["Client"] = IdentityClient;
                return Redirect(url);
            }
            catch (Exception e)
            {
                TempData["error"] = e.Message;
                return View("Login");
            }

        }

        public ActionResult Register()
        {
            return View();
        }

        [HttpPost]
        public JsonResult GetVerificationCode(string sender)
        {
            JsonResult res = new JsonResult();
            var success = "";
            var error = "";
            try
            {
                TempData["code"] = mmail.SendMailLocalhost(sender);
                success = "邮件发送成功";
            }
            catch (Exception ex)
            {
                error = ex.Message;
            }

            res.Data = new { success, error };
            return res;
        }
        
        private void SendMailLocalhost(string sender, int code)
        {
            
            MailMessage msg = new MailMessage();
            msg.To.Add(sender);
            msg.From = new MailAddress("15923123802@163.com", "魅商官方邮件", System.Text.Encoding.UTF8);
            msg.Subject = "注册邮件";
            msg.SubjectEncoding = System.Text.Encoding.UTF8;
            msg.Body = code.ToString();
            msg.BodyEncoding = System.Text.Encoding.UTF8;
            msg.IsBodyHtml = false;
            msg.Priority = MailPriority.High;

            SmtpClient client = new SmtpClient();
            client.UseDefaultCredentials = true;
            client.Credentials = new System.Net.NetworkCredential("15923123802@163.com", "zqq20170101");
            client.Host = "smtp.163.com";
            client.Port = 25;
            client.EnableSsl = true;
            client.Send(msg);
        }

        [HttpPost]
        public ActionResult RegisterUser(ClientModel client,int code)
        {
            if((int)TempData["code"] == code)
            {
                _aService.RegisterClient(client.Name, client.Password, client.E_mail);
                return RedirectToAction("Login");
            }
            else
            {
                return View("Register");
            }
           
            
        }

        [HttpPost]
        public JsonResult Logout()
        {
            Session["Client"] = null;
            var res = new JsonResult();
            string success = "logout";
            res.Data = new { success };
            return res;
        }
    }
}